package com.lcp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcp.common.Result;
import com.lcp.entity.LectureRegistration;
import com.lcp.entity.User;
import com.lcp.service.LectureRegistrationService;
import com.lcp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/lecture-registrations")
public class LectureRegistrationController {

    @Autowired
    private LectureRegistrationService lectureRegistrationService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result<LectureRegistration> getById(@PathVariable Long id) {
        return Result.success(lectureRegistrationService.getById(id));
    }

    @GetMapping("/page")
    public Result<IPage<LectureRegistration>> pageByLectureId(
            @RequestParam Long lectureId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(lectureRegistrationService.pageByLectureId(lectureId, pageNum, pageSize));
    }

    @GetMapping("/current")
    public Result<List<LectureRegistration>> getCurrentUserRegistrations() {
        User currentUser = userService.getCurrentUser();
        return Result.success(lectureRegistrationService.listByUserId(currentUser.getId()));
    }

    @PostMapping("/{lectureId}")
    public Result<LectureRegistration> register(@PathVariable Long lectureId) {
        User currentUser = userService.getCurrentUser();
        LectureRegistration registration = new LectureRegistration();
        registration.setLectureId(lectureId);
        registration.setUserId(currentUser.getId());
        return Result.success(lectureRegistrationService.create(registration));
    }

    @PostMapping("/{lectureId}/cancel")
    public Result<Void> cancelRegistration(@PathVariable Long lectureId) {
        User currentUser = userService.getCurrentUser();
        lectureRegistrationService.cancelRegistration(lectureId, currentUser.getId());
        return Result.success();
    }

    @PostMapping("/{lectureId}/attend")
    public Result<Void> markAsAttended(@PathVariable Long lectureId) {
        User currentUser = userService.getCurrentUser();
        lectureRegistrationService.markAsAttended(lectureId, currentUser.getId());
        return Result.success();
    }

    @PostMapping("/{lectureId}/feedback")
    public Result<Void> submitFeedback(
            @PathVariable Long lectureId,
            @RequestParam String feedback,
            @RequestParam Integer rating) {
        User currentUser = userService.getCurrentUser();
        lectureRegistrationService.submitFeedback(lectureId, currentUser.getId(), feedback, rating);
        return Result.success();
    }

    @GetMapping("/{lectureId}/check")
    public Result<Boolean> checkRegistration(@PathVariable Long lectureId) {
        User currentUser = userService.getCurrentUser();
        return Result.success(lectureRegistrationService.isRegistered(lectureId, currentUser.getId()));
    }
} 